<template>
  <div>
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="article-container" v-if="article">
      <h1 class="art-title">{{ article.title }}</h1>
      <van-cell
        center
        :title="article.aut_name"
        :label="article.pubdate | dateFormat"
      >
        <template #icon>
          <img :src="article.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="article.is_followed"
              @click="setUnFollow"
              >已关注</van-button
            >
            <van-button
              type="info"
              size="mini"
              icon="plus"
              plain
              v-else
              @click="setFollow"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>
      <van-divider></van-divider>
      <div class="art-content" v-html="article.content"></div>
      <van-divider>End</van-divider>
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="article.attitude === 1"
          >已点赞</van-button
        >

        <van-button icon="good-job-o" type="danger" size="small" v-else
          >点赞</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import {
  getArticleDetailAPI,
  followUserAPI,
  unfollowUserAPI
} from '@/api/articleAPI.js'
export default {
  name: 'ArticleDetail',
  data() {
    return {
      article: null
    }
  },
  props: ['id'],

  components: {},

  computed: {},
  created() {
    this.initArticleDetail()
  },

  mounted() {
    console.log('id:', this.$route.params.id)
  },

  methods: {
    async initArticleDetail() {
      const { data: res } = await getArticleDetailAPI(this.id)
      console.log('res:', res)
      if (res.message === 'OK') {
        this.article = res.data
      }
    },
    async setFollow() {
      const { data: res } = await followUserAPI(this.article.aut_id)
      if (res.message === 'OK') {
        this.$toast.success('关注成功')
        this.article.is_followed = true
      }
    },
    async setUnFollow() {
      const res = await unfollowUserAPI(this.article.aut_id)
      if (res.status === 204) {
        this.$toast.success('取消关注成功')
        this.article.is_followed = false
      }
    }
  }
}
</script>
<style lang="less" scoped>
.article-container {
  padding: 20px;
  margin-top: 92px;
  background-color: #fff;
}
.art-title {
  font-size: 32px;
  font-weight: bold;
  margin: 20px 0;
}

.art-content {
  font-size: 24px;
  line-height: 48px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 10px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 10px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
